package listbyorder.access101_200.test151;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/13 8:42
 */
public class Solution1 {

    public String reverseWords(String s) {
        return process(s, 0, s.length() - 1).trim();
    }

    private String process(String s, int start, int end) {
        if (start > end) return "";
        String ans = "";
        int index = end;
        while (index >= 0 && s.charAt(index) == ' ') {
            index--;
        }
        int cur = index;
        while (cur >= 0 && s.charAt(cur) != ' ') {
            cur--;
        }
        ans += s.substring(cur + 1, index + 1) + " ";
        ans += process(s, start, cur);
        return ans;
    }
}
